package com.itheima.controller;
import com.alibaba.dubbo.config.annotation.Reference;

import com.itheima.VO.Member;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.memberService;
import com.itheima.service.orderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.List;
import java.util.Map;


import java.util.Set;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private memberService memberService;
    @Reference
    private com.itheima.service.orderService orderService;
    @RequestMapping("/findPageMMM")
    public Result findPageMMM(@RequestBody QueryPageBean queryPageBean) {

        try {
            PageResult pageResult = memberService.findPageMMM(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MEMBER_CLASSIFY_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_CLASSIFY_FAIL);
        }
    }

    @RequestMapping("/findOne")
    public Result findOne(Integer id) {

        try {
            com.itheima.POJO.Member member = memberService.findOne(id);
            return new Result(true, MessageConstant.GET_MEMBER_CLASSIFY_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_CLASSIFY_FAIL);
        }
    }



    @RequestMapping("/findMedicalInformation")
    public Result findMedicalInformation(Integer id) {

        try {
            Map<String, Object> map = memberService.findMedicalInformationById(id);
            return new Result(true, MessageConstant.GET_MEDICAL_INFORMATION_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEDICAL_INFORMATION_FAIL);
        }
    }

    @RequestMapping("/findHealthProgramIds")
    public Result findHealthProgramIds(Integer id) {
        try {
            List<Integer> list = memberService.findHealthProgramIdsById(id);
            return new Result(true, "成功", list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "失败");
        }
    }

    @RequestMapping("/healthEdit")
    public Result healthEdit(@RequestBody Map map, Integer[] healthProgramIds) {
        try {
            memberService.healthEdit(map, healthProgramIds);
            return new Result(true, MessageConstant.EDIT_HEALTH_PEOPLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_HEALTH_PEOPLE_FAIL);
        }
    }

    @RequestMapping("/deletePeople")
    public Result deletePeople(Integer id) {
        try {
            memberService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Member member) {
        System.out.println(111);
        try {
            memberService.add(member);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
        return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = memberService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return page;
    }

    @RequestMapping("/findMemberById")
    public Result findMemberById(Integer id) {
        try {
            Member member = memberService.findMemberById(id);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, member);
          } catch (Exception e) {
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/editMemberById")
    public Result editMemberById(@RequestBody Member member){
        try {
            memberService.editMember(member);
        } catch (Exception e) {
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_MEMBER_SUCCESS);
    }

    @RequestMapping("delete")
    public Result delete(Integer id){
        try {
            memberService.delete(id);
        } catch (Exception e) {
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MEMBER_SUCCESS);
    }

    @RequestMapping("/getMemberCount")
    public Result getMemberCount(@RequestBody List<String> list) {
        try {
            List<String> result = new ArrayList<>();
            String start = list.get(0);
            String end = list.get(1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(sdf.parse(start));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH),1);
            max.setTime(sdf.parse(end));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
            List<Integer> memberCountList=memberService.findMemberCountByMouth(result);

            Map<String, Object> map = new HashMap<>();
            map.put("months",result);
            map.put("memberCount",memberCountList);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (ParseException e) {
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }

    @RequestMapping("/getOrderCount")
    public Result getOrderCount(@RequestBody List<String> list) {
        try {
            List<String> result = new ArrayList<>();
            String start = list.get(0);
            String end = list.get(1);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");//格式化为年月
            Calendar min = Calendar.getInstance();
            Calendar max = Calendar.getInstance();
            min.setTime(sdf.parse(start));
            min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH),1);
            max.setTime(sdf.parse(end));
            max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);
            Calendar curr = min;
            while (curr.before(max)) {
                result.add(sdf.format(curr.getTime()));
                curr.add(Calendar.MONTH, 1);
            }
            List<Integer> orderCountList=orderService.findOrderCountByMouth(result);

            Map<String, Object> map = new HashMap<>();
            map.put("months",result);
            map.put("orderCount",orderCountList);
            return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS, map);
        } catch (ParseException e) {
            return new Result(false, MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL);
        }
    }
}

